using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ColliderDrawer : MonoBehaviour {
#if UNITY_EDITOR
    public bool showGizmos = true;

    public Color boxColor = Color.yellow;
    public Color polygonColor = Color.green;
    public Color circleColor = Color.red;
    //public Color deadColor = Color.red;

    void DrawBoxCollider2D() {
        Gizmos.color = boxColor;
        BoxCollider2D[] boxs = FindObjectsOfType<BoxCollider2D>();
        foreach (var box in boxs) {
            //SpecificColor<DeadTrigger>(box, deadColor);
            DrawBoxCollider2D(box);
            Gizmos.color = boxColor;
        }
    }

    void DrawBoxCollider2D(BoxCollider2D box) {

            var size = box.size;
        var rx = size.x * 0.5f;
        var ry = size.y * 0.5f;
        var scale = box.transform.lossyScale;
        var rotation = box.transform.rotation;
        var offset = box.transform.position;
        Vector3[] v = new Vector3[4];
        v[0] = rotation * new Vector3(-rx * scale.x + box.offset.x,
                                      -ry * scale.y + box.offset.y, 0) + offset;
        v[1] = rotation * new Vector3(-rx * scale.x + box.offset.x,
                                      ry * scale.y + box.offset.y, 0) + offset;
        v[2] = rotation * new Vector3(rx * scale.x + box.offset.x,
                                      ry * scale.y + box.offset.y, 0) + offset;
        v[3] = rotation * new Vector3(rx * scale.x + box.offset.x,
                                      -ry * scale.y + box.offset.y, 0) + offset;

        Gizmos.DrawLine(v[0], v[1]);
        Gizmos.DrawLine(v[1], v[2]);
        Gizmos.DrawLine(v[2], v[3]);
        Gizmos.DrawLine(v[3], v[0]);

    }

    void DrawPolygonCollider2D() {
        Gizmos.color = polygonColor;
        PolygonCollider2D[] polygons = FindObjectsOfType<PolygonCollider2D>();
        foreach (var polygon in polygons) {
            //SpecificColor<DeadTrigger>(polygon, deadColor);
            DrawPolygonCollider2D(polygon);
            Gizmos.color = polygonColor;
        }
    }

    void DrawPolygonCollider2D(PolygonCollider2D polygon) {
        var scale = polygon.transform.lossyScale;
        var rotation = polygon.transform.rotation;
        var offset = polygon.transform.position;
        for (int i = 0, len = polygon.pathCount; i < len; i++) {
            var v = polygon.GetPath(i);
            for (int j = 0, len2 = v.Length - 1; j < len2; j++) {
                var v1 = rotation * new Vector3(v[j].x * scale.x + polygon.offset.x,
                                                v[j].y * scale.y + polygon.offset.y, 0) + offset;
                var v2 = rotation * new Vector3(v[j + 1].x * scale.x + polygon.offset.x,
                                                v[j + 1].y * scale.y + polygon.offset.y, 0) + offset;
                Gizmos.DrawLine(v1, v2);
            }
            var v3 = rotation * new Vector3(v[v.Length - 1].x * scale.x + polygon.offset.x,
                                            v[v.Length - 1].y * scale.y + polygon.offset.y, 0) + offset;
            var v4 = rotation * new Vector3(v[0].x * scale.x + polygon.offset.x,
                                            v[0].y * scale.y + polygon.offset.y, 0) + offset;
            Gizmos.DrawLine(v3, v4);
        }
    }

    void DrawCircleCollider2D() {
        Gizmos.color = circleColor;
        CircleCollider2D[] circles = FindObjectsOfType<CircleCollider2D>();
        foreach (var circle in circles) {
            //SpecificColor<DeadTrigger>(circle, deadColor);
            DrawCircleCollider2D(circle);
            Gizmos.color = circleColor;
        }
    }

    void DrawCircleCollider2D(CircleCollider2D circle) {

            Gizmos.DrawWireSphere(circle.offset + (Vector2)circle.transform.position,
                              circle.radius * circle.transform.lossyScale.x);
    }

    //void SpecificColor<T>(Collider2D collider, Color color) where T : MonoBehaviour {
    //    if (collider.GetComponent<DeadTrigger>() != null) {
    //        Gizmos.color = color;
    //    }
    //}

    void OnDrawGizmos() {
        if (!showGizmos) {
            return;
        }

        Color color = Gizmos.color;

        DrawBoxCollider2D();
        DrawPolygonCollider2D();
        DrawCircleCollider2D();

        Gizmos.color = color;
    }
#endif
}
